<template>
  <table>
    
  </table>
</template>

<script>
import PriceInfo from "@/common/PriceInfo.js";
import {
  showPsgType,
  showIdTypeDesc,
  showGenderType,
} from "@/common/common.js";

import { updateFlightOrderPassenger, convertPriceInfo } from "@/api/flight.js";
import { createChangeOrderMulti } from "@/api/flight-change.js";
import { createRefundOrderMulti } from "@/api/flight-refund.js";
import { sendFlightOrderSms } from "@/api/sms.js";


export default {
  props: {
    detail: {
      required: true,
      type: Object,
    },
  },
  components: {
    // MyMod?alPrompt
    
  },
  data() {
    return {
      modalTitle: "",
      modalTitle2: "",
      modalNullable: false,
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
    customerId: function () {
      if (
        this.detail === null ||
        this.detail.customer === undefined ||
        this.detail.customer === null
      ) {
        return 0;
      } else {
        return this.detail.customer.id;
      }
    },
  },
  mounted: function () {},
  activated: function () {
    console.log("customer-login-info: activated");
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      this.$store.dispatch("showLoading", { loadingText: msg });
    },
    hideLoading: function () {
      this.$store.dispatch("hideLoading");
    },
    commonShowMessage: function (v) {
      if (v.status === "OK") {
        this.showErrMsg("操作成功");
        this.$emit("refresh");
      } else {
        this.showErrMsg("操作失败: " + v.errmsg);
      }
    },
    showPsgType: function (psgType) {
      return showPsgType(psgType);
    },
    showIdTypeDesc: function (idType) {
      return showIdTypeDesc(idType);
    },
    showGenderType: function (gender) {
      return showGenderType(gender);
    },
    editPsg: function (index) {
      this.$parent.$refs.updatePassengerModal
        .modal(this.detail.passengers[index])
        .then((info) => {
          updateFlightOrderPassenger(this.detail.id, info, (v) =>
            this.commonShowMessage(v)
          );
        })
        .catch((ex) => {});
    },
    changeTicket: function (index) {
      // 多人同时签转
      const psgInfo = this.detail.passengers[index];
      const psgType = psgInfo.psgType;

      let psgPrice = new PriceInfo();
      convertPriceInfo(this.detail.prices, psgType, psgPrice);

      // 找到同类型的乘机人：成人，儿童，婴儿
      const psgList = [];
      let onlyOneTicketAllowed = false;
      for (const info of this.detail.passengers) {
        if (info.psgType === psgType) {
          if (info.ticketNos.length == 0) {
            psgList.push({
              ticketNo: info.ticketNo,
              name: info.name,
              idNo: info.idNo,
              selected: false,
            });
          } else {
            onlyOneTicketAllowed = true;
            for (const ticketNo of info.ticketNos) {
              psgList.push({
                ticketNo: ticketNo,
                name: info.name,
                idNo: info.idNo,
                selected: false,
              });
            }
          }
        }
      }

      const params = {
        customerId: this.customerId,
        orderId: this.detail.id,
        orderNo: this.detail.orderNo,
        intlTicket: this.detail.intlTicket,
        oldPnrNo: this.detail.pnrNo,
        amount: psgPrice.amount,
        cost: psgPrice.cost,
        parvalue: psgPrice.parValue,
        tax: psgPrice.tax,
        flights: this.detail.flights,
        onlyOneTicketAllowed: onlyOneTicketAllowed,
      };
      this.$parent.$refs.changeTicketModal
        .modal(psgList, params)
        .then((v) => this.doCreateChangeOrder(v))
        .catch((ex) => {});
    },
    doCreateChangeOrder: function (info) {
      createChangeOrderMulti(JSON.stringify(info), (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("操作成功");
          this.$router.push("/flt/change/order/" + jsonResult.returnCode);
        }
      });
    },
    multiRefundTicket: function (index) {
      // 多人同时退废
      const psgInfo = this.detail.passengers[index];
      const psgType = psgInfo.psgType;

      let psgPrice = new PriceInfo();
      convertPriceInfo(this.detail.prices, psgType, psgPrice);

      // 找到同类型的乘机人：成人，儿童，婴儿
      const psgList = [];
      for (const info of this.detail.passengers) {
        if (info.psgType === psgType) {
          psgList.push({
            ticketNo: info.ticketNo,
            name: info.name,
            idNo: info.idNo,
            selected: false,
          });
        }
      }

      this.$parent.$refs.multiRefundTicketModal
        .modal(
          this.customerId,
          psgList,
          this.id,
          this.detail.orderNo,
          this.detail.flights,
          this.detail.intlTicket,
          psgPrice.amount,
          psgPrice.cost,
          psgPrice.parValue,
          psgPrice.tax
        )
        .then((info) => {
          this.doCreateRefundOrderMulti(info);
        })
        .catch((ex) => {});
    },
    doCreateRefundOrderMulti: function (info) {
      createRefundOrderMulti(JSON.stringify(info), (jsonResult) => {
        if (jsonResult.status !== "OK") {
          this.showErrMsg(jsonResult.errmsg, "danger");
        } else {
          this.showErrMsg("操作成功");
          this.$router.push("/flt/refund/order/" + jsonResult.returnCode);
        }
      });
    },
    sendSms: function (index) {
      const psgInfo = this.detail.passengers[index];
      this.$parent.modalTitle = "请输入接收短信的手机号（默认使用乘机人号码）：";
      this.modalNullable = true;
      this.$parent.$refs.modalSms
        .modal(2, this.detail.id, psgInfo.id)
        .then((v) => {
          sendFlightOrderSms(
            this.detail.id,
            {
              psgId: psgInfo.id,
              mobileNo: v.mobile,
              smsContent: v.smsContent,
              smsFormat: v.smsFormat,
            },
            (v) => {
              this.commonShowMessage(v);
            }
          );
        })
        .catch((ex) => {});
    },
  },
};
</script>